﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using DslModeling=global::Microsoft.VisualStudio.Modeling;
using DslDesign=global::Microsoft.VisualStudio.Modeling.Design;
using DslDiagrams=global::Microsoft.VisualStudio.Modeling.Diagrams;
using DslValidation=global::Microsoft.VisualStudio.Modeling.Validation;

namespace Salamanca.Sdml
{

    [DslValidation.ValidationState(DslValidation.ValidationState.Enabled)]
    partial class EntityAttribute:
        IAttribute
    {
        partial class AllowNullPropertyHandler
        {
            protected override void OnValueChanged(EntityAttribute element, bool oldValue, bool newValue)
            {
                foreach (DslDiagrams.PresentationElement pe in DslDiagrams.PresentationViewsSubject.GetPresentation(element.DomainEntity))
                {
                    DslDiagrams.ShapeElement shape=pe as DslDiagrams.ShapeElement;
                    if (shape!=null)
                        shape.Invalidate();
                }

                base.OnValueChanged(element, oldValue, newValue);
            }
        }

        partial class UniquePropertyHandler
        {
            protected override void OnValueChanged(EntityAttribute element, bool oldValue, bool newValue)
            {
                foreach (DslDiagrams.PresentationElement pe in DslDiagrams.PresentationViewsSubject.GetPresentation(element.DomainEntity))
                {
                    DslDiagrams.ShapeElement shape=pe as DslDiagrams.ShapeElement;
                    if (shape!=null)
                        shape.Invalidate();
                }

                base.OnValueChanged(element, oldValue, newValue);
            }
        }

        [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification="Domain property custom storage")]
        public string GetNameAndTypeValue()
        {
            return this.Name.Trim()+" : "+this.Type.Trim();
        }

        public void SetNameAndTypeValue(string newValue)
        {
            string[] separated=newValue.Split(new char[] { ':' });
            if (separated.Length>0)
                this.Name=separated[0].Trim();
            if (separated.Length>1)
                this.Type=separated[1].Trim();
        }

        [DslValidation.ValidationMethod(DslValidation.ValidationCategories.Menu|DslValidation.ValidationCategories.Open|DslValidation.ValidationCategories.Save)]
        private void ValidateName(DslValidation.ValidationContext context)
        {
            if (string.Compare("Type", Name, true)==0)
                context.LogError(Resources.SR.AttributeNameCannotBeType, "SDML05", this);
        }

        [DslValidation.ValidationMethod(DslValidation.ValidationCategories.Menu|DslValidation.ValidationCategories.Open|DslValidation.ValidationCategories.Save)]
        private void ValidateType(DslValidation.ValidationContext context)
        {
            if (!TypesProviderConverter.GetAllowedTypes(Store).Contains(Type))
                context.LogWarning(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.SR.InvalidType,
                        Type
                     ),
                    "SDML08",
                    this
                );
        }

        public bool IsEnumType
        {
            get
            {
                return Partition.ElementDirectory.FindElements<DomainEnum>().FirstOrDefault<DomainEnum>(e => e.Name==Type)!=null;
            }
        }

        public DataColumn DataColumn
        {
            get
            {
                DomainEntity.DomainModel.LoadDataSet();
                return (DataColumn)_ColumnReference.Target;
            }
            set
            {
                _ColumnReference=new WeakReference(value);
            }
        }

        private WeakReference _ColumnReference;
    }
}
